package cn.tedu.netctoss.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.tedu.netctoss.dao.CostDao;
import cn.tedu.netctoss.entity.Cost;
//localhost:8080/NETCTOSS/cost
@RequestMapping("cost")
@Controller
public class CostController {
	//使用自动装配初始化CostDao接口
	@Autowired
	private CostDao costDao;

	//localhost:8080/NETCTOSS/cost/findCost.do
	@RequestMapping("findCost.do")
	public String findCost(ModelMap modelMap){
		//调用CostDao获取数据
		List<Cost>costs=costDao.findCost();
		//将数据转发给/WEB-INF/cost/find.jsp
		modelMap.addAttribute("costs",costs);
		return "cost/find";
	}

	@RequestMapping("toAddCost.do")
	public String toAddCost(){
		return "cost/add";
	}

	@RequestMapping("addCost.do")
	public String addCost(Cost cost){
		Integer rows=costDao.addCost(cost);
		//添加成功，重定向到findCost.do
		if(rows>0){
			return "redirect:findCost.do";
		}
		return null;
	}

	@RequestMapping("toUpdateCost.do")
	public String toUpdateCost(Integer id,ModelMap modelMap){
		Cost cost = costDao.findById(id);
		System.out.println("cost="+cost);
		modelMap.addAttribute("cost",cost);
		return "cost/update";
	}
}
